package com.example.utils;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Locale;

import android.content.Context;

public final class FileUtils {

	private FileUtils() {
	}
	
	public static interface LineReaderCallback{
		void onReadline(String line) throws Exception;
	}
	public static void lineReader(Reader r, LineReaderCallback callback)throws Exception{
		BufferedReader br = new BufferedReader(r);
		String line;
		while((line=br.readLine())!=null){
			callback.onReadline(line);
		}
		br.close();
	}
	

	/**
	 * 创建文件夹，按路径创建
	 */
	public static boolean createDir(String path) {
		return createDir(new File(path));
	}

	public static boolean createDir(File file) {
		if (file.exists()) {
			if (file.isDirectory())
				return true;
			file.delete(); // 避免他是一个文件
		}
		if (file.mkdirs())
			return true;
		return false;
	}

	/**
	 * 删除一个文件/文件夹
	 */
	public static boolean remove(String path) {
		return remove(new File(path));
	}

	public static boolean remove(File file) {
		if (!file.exists())
			return true;
		if (file.isDirectory()) {
			File[] files = file.listFiles();
			if (files != null) {
				for (File f : files) {
					if (f.isDirectory())
						remove(f);
					else
						f.delete();
				}
			}
		}
		return file.delete();
	}

	/**
	 * 在打开一个文件写数据之前，先检测该文件路径的父目录是否已创建，保证能创建文件
	 */
	public static void checkParentPath(File file) {
		File parent = file.getParentFile();
		if (parent != null && !parent.isDirectory())
			createDir(parent);
	}

	public static void checkParentPath(String path) {
		checkParentPath(new File(path));
	}

	/**
	 * 写字节数组到文件
	 */
	public static boolean writeBytesToFile(File file, byte[] data, int offset,
			int length, boolean isAppendToFile) {
		checkParentPath(file);

		if (data == null)
			return false;

		if (length < 0)
			return false;

		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file, isAppendToFile);
			fos.write(data, offset, length);
			fos.flush();
			return true;
		} catch (Exception e) {
		} finally {
			try {
				fos.close();
			} catch (Exception e) {
			}
		}
		return false;
	}

	public static boolean writeBytesToFile(File file, byte[] data,
			boolean isAppendToFile) {
		return writeBytesToFile(file, data, 0, data.length, isAppendToFile);
	}

	public static boolean writeBytesToFile(File file, byte[] data) {
		return writeBytesToFile(file, data, 0, data.length, false);
	}

	public static boolean writeBytesToFile(String path, byte[] data,
			int offset, int length, boolean isAppendToFile) {
		return writeBytesToFile(new File(path), data, offset, length,
				isAppendToFile);
	}

	public static boolean writeStringToFile(File file, String string) {
		return writeBytesToFile(file, string.getBytes());
	}

	/**
	 * 读取文件内容到byte数组
	 */
	public static byte[] readFileToBytes(String path) {
		return readFileToBytes(new File(path));
	}

	public static byte[] readFileToBytes(File file) {
		InputStream is = null;
		try {
			is = new FileInputStream(file);
			return readStreamToBytes(is);
		} catch (Exception e) {
		} finally {
			try {
				is.close();
			} catch (Exception e) {
			}
		}
		return null;
	}

	public static byte[] readStreamToBytes(InputStream is) {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		try {
			byte[] buf = new byte[1024 * 4];
			int len;
			while ((len = is.read(buf)) != -1) {
				bos.write(buf, 0, len);
			}
		} catch (IOException e) {
			return null;
		}
		return bos.toByteArray();
	}

	public static String readStreamToString(InputStream is) {
		byte[] data = readStreamToBytes(is);
		return data != null ? new String(data) : null;
	}

	public static String readFileToString(String file) {
		return readFileToString(new File(file));
	}
	public static String readFileToString(File file) {
		byte[] data = readFileToBytes(file);
		return data != null ? new String(data) : null;
	}

	/**
	 * 转换文件大小
	 */
	public static String coverSize(long size) {
		String s = "";
		if (size < 1024)
			s += size + "b";
		else if (size < 1024 * 1024) {
			s = String.format(Locale.getDefault(), "%.1fK", size / 1024f);
		} else if (size < 1024 * 1024 * 1024) {
			s = String
					.format(Locale.getDefault(), "%.1fM", size / 1024 / 1024f);
		} else {
			s = String.format(Locale.getDefault(), "%.1fG",
					size / 1024 / 1024 / 1024f);
		}
		return s;
	}

	/**
	 * 获取/data/data/应用私有目录下的文件夹绝对路径，末尾带 "/"，不创建
	 */
	public static String getDirPathInPrivate(Context context, String name) {
		return context.getDir(name, Context.MODE_PRIVATE).getAbsolutePath()
				+ File.separator;
	}
}
